Skip to content

WidgetsDetailsModel

Base classes

Name Children Inherits
AbstractTableModel
prettyqt.core.abstracttablemodel
Abstract model that can be subclassed to create table models.

â‹” Inheritance diagram

graph TD
  4261135213["itemmodels.WidgetsDetailsModel"]
  656210031["core.AbstractTableModel"]
  4092674192["core.AbstractTableModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  232985924["QtCore.QAbstractTableModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  656210031 --> 4261135213
  4092674192 --> 656210031
  3882849576 --> 4092674192
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  2751680898 --> 3882849576
  2751680898 --> 4092674192
  232985924 --> 656210031
  1898060022 --> 232985924
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: AbstractTableModel

Source code in prettyqt/itemmodels/widgetsdetailsmodel.py
class WidgetsDetailsModel(core.AbstractTableModel):
    SUPPORTS = Sequence[core.QObject]

    def __init__(self, items: Sequence[core.QObject], **kwargs):
        super().__init__(**kwargs)
        self.items = items
        common_ancestor = classhelpers.find_common_ancestor([type(i) for i in self.items])
        logger.debug("%s: found common ancestor %s", type(self).__name__, common_ancestor)
        metaobject = common_ancestor.staticMetaObject  # ty: ignore[missing-attribute]
        self.props = core.MetaObject(metaobject).get_properties(only_stored=True)
        self.props.sort(key=lambda x: x.get_name())

    @classmethod
    def supports(cls, instance) -> bool:
        match instance:
            case (core.QObject(), *_):
                return True
            case _:
                return False

    def columnCount(self, parent: core.ModelIndex | None = None) -> int:
        parent = parent or core.ModelIndex()
        return 0 if parent.isValid() else len(self.props)

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ) -> str | None:
        match orientation, role:
            case constants.VERTICAL, constants.DISPLAY_ROLE if section < len(self.items):
                return repr(self.items[section])
            case constants.HORIZONTAL, constants.DISPLAY_ROLE if section < len(
                self.props
            ):
                return self.props[section].get_name()
        return None

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not index.isValid():
            return None
        prop = self.props[index.column()]
        widget = self.items[index.row()]
        value = prop.read(widget)
        match role:
            case constants.CHECKSTATE_ROLE if isinstance(value, bool):
                return self.to_checkstate(value)
            case constants.DISPLAY_ROLE if not isinstance(value, bool):
                return self.to_display_text(value)
            case constants.EDIT_ROLE | constants.USER_ROLE:
                return value
        return None

    def setData(
        self,
        index: core.ModelIndex,
        value,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
    ) -> bool:
        if not index.isValid():
            return False
        prop = self.props[index.column()]
        widget = self.items[index.row()]
        match role:
            case constants.USER_ROLE | constants.EDIT_ROLE:
                pass
            case constants.CHECKSTATE_ROLE:
                value = self.from_checkstate(value)
            case _:
                return False
        if not prop.write(widget, value):
            return False
        self.dataChanged.emit(index, index)
        return True

    def rowCount(self, parent: core.ModelIndex | None = None) -> int:
        """Override for AbstractitemModel base method."""
        parent = parent or core.ModelIndex()
        return 0 if parent.isValid() else len(self.items)

    def flags(self, index: core.ModelIndex) -> constants.ItemFlag:
        flags = super().flags(index)
        if not index.isValid():
            return flags
        prop = self.props[index.column()]
        if not prop.isWritable():
            return flags
        if prop.get_python_type() is bool:
            return flags | constants.IS_CHECKABLE
        return flags | constants.IS_EDITABLE

rowCount(parent: core.ModelIndex | None = None) -> int

Override for AbstractitemModel base method.

Source code in prettyqt/itemmodels/widgetsdetailsmodel.py
def rowCount(self, parent: core.ModelIndex | None = None) -> int:
    """Override for AbstractitemModel base method."""
    parent = parent or core.ModelIndex()
    return 0 if parent.isValid() else len(self.items)

:fa-brands-github: Show source on GitHub